package com.ruizhong.PIW.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruizhong.PIW.service.FileService;
import com.ruizhong.PIW.utils.MinioUtilS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FileServiceImpl implements FileService {
    private static final ObjectMapper mapper = new ObjectMapper();
    @Autowired
    private MinioUtilS minioUtilS;
    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;

    @Override
    public String uploadFile(MultipartFile file) {
//        long size = file.getSize();
//        if (size>1048576l){
//            throw new RuntimeException("照片大小不可超过1MB");
//        }
        List<String> upload = minioUtilS.upload(new MultipartFile[]{file});
        String url = address + "/" + bucketName + "/" + upload.get(0);
        log.info("上传成功");
        return url;
    }

    @Override
    public void deleteFile(List<String> fileList) throws Exception {
        List<String> filePaths = fileList.stream()
                .map(filePath -> filePath.substring(filePath.lastIndexOf("/") + 1))
                .collect(Collectors.toList());
        minioUtilS.removeObjects(bucketName, filePaths);
        log.info("删除成功");


    }

}
